package com.zjitc.controller;

import com.zjitc.model.Country;
import com.zjitc.service.CountryService;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.view.InternalResourceView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/20 15:46
 * @description:
 */
@Controller
public class CountryController {

  @Autowired
  public CountryService countryService;

  @RequestMapping("/findAll")
  public ModelAndView findAll(){
    List<Country> list = countryService.findAll();
    ModelAndView mv = new ModelAndView();
    mv.addObject("countryList",list);
    mv.setViewName("/views/display.jsp");
    return mv;
  }

  @RequestMapping("/findRegion")
  public ModelAndView findRegion(Country country){
    String region=country.getRegion();
    List<Country> list=countryService.findRegion(region);
    ModelAndView mv=new ModelAndView();
    mv.addObject("countryList",list);
    mv.setViewName("/views/display.jsp");
    return mv;
  }

  @RequestMapping("/add")
  public ModelAndView add(Country country)
      throws IOException {
    ModelAndView mv = new ModelAndView();
    int add = countryService.add(country);
    mv.addObject("countryList",add);
    mv.setView(new InternalResourceView("/findAll.do"));
    return mv;
  }

  @RequestMapping("/findByCode")
  public ModelAndView findByCode(String code){
    ModelAndView mv=new ModelAndView();
    Country list=countryService.findByCode(code);
    mv.addObject("codeList",list);
    mv.setViewName("/views/update.jsp");
    return mv;
  }

  @RequestMapping("/update")
  public ModelAndView update(Country country){
    ModelAndView mv=new ModelAndView();
    int update = countryService.update(country);
    mv.addObject("updateList",update);
    mv.setView(new InternalResourceView("/findAll.do"));
    return mv;
  }

  @RequestMapping("/delete")
  public ModelAndView delete(String code){
    ModelAndView mv=new ModelAndView();
    int list=countryService.delete(code);
    mv.addObject("deleteList",list);
    mv.setView(new InternalResourceView("/findAll.do"));
    return mv;
  }
}
